package com.hxh.wechat.adpter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.hxh.wechat.R
import com.hxh.wechat.util.EmoJiUtils

class EmojiAdapter(private val emojiList: List<String>, private val listener: (String) -> Unit) :
    RecyclerView.Adapter<EmojiAdapter.EmojiViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): EmojiViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_emoji, parent, false)
        return EmojiViewHolder(view)
    }

    override fun onBindViewHolder(holder: EmojiViewHolder, position: Int) {
        val emoji = emojiList[position]
        holder.bind(emoji)
        holder.itemView.setOnClickListener { listener.invoke(emoji) }
    }

    override fun getItemCount(): Int {
        return emojiList.size
    }

    inner class EmojiViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val emojiTextView: TextView = itemView.findViewById(R.id.emoji_text_view)

        fun bind(emoji: String) {
            emojiTextView.setBackgroundResource(EmoJiUtils.allMap.get(emoji)!!)
        }
    }
}